package com.lufei.dh.bottom.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hugocastelani.waterfalltoolbar.library.WaterfallToolbar;
import com.lufei.dh.R;
import com.lufei.dh.bottom.Adapter.AdapterLF;
import com.lufei.dh.more.Bean.BeanMore;
import com.lufei.dh.view.WebActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.lufei.dh.view.Mult.CateActivity.KJ;
import static com.lufei.dh.view.Mult.CateActivity.getJson;

/**
 * 版权：易金卡沃 版权所有
 * <p>
 * 作者：lufei
 * <p>
 * 创建日期：2018/6/25
 * <p>
 * 描述：
 * <p>
 * 修订历史：
 */
public class Fragment3 extends Fragment {
    @BindView(R.id.f3_recyclerview)
    RecyclerView f3Recyclerview;
    Unbinder unbinder;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.waterfall_toolbar)
    WaterfallToolbar waterfallToolbar;
    private List<Integer> integers = new ArrayList<>();
    private List<BeanMore> data = new ArrayList<>();
    AdapterLF adapterLF;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        return super.onCreateView(inflater, container, savedInstanceState);
        Log.e("lufei", "3333");
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment3, container, false);
        unbinder = ButterKnife.bind(this, view);
        setList();
        init();
        return view;
    }

    private void init() {
        adapterLF = new AdapterLF(data, getContext());
        f3Recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        f3Recyclerview.setAdapter(adapterLF);
//        waterfallToolbar.addRecyclerView(f3Recyclerview);
        setAdapterLF();
    }
    private void setAdapterLF(){
        adapterLF.setOnPlayClickListener(new AdapterLF.OnPlayClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent=new Intent();
                intent.putExtra("url",data.get(position).getUrl());
                intent.setClass(getContext(), WebActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("lufei", "3333......");
    }

    private void setList() {
//        String s1 = this.getString(R.string.json);
        String s1 = getJson("more.json", getContext());
        try {
            JSONObject jsonObject = new JSONObject(s1);
            JSONObject result = jsonObject.optJSONObject("result");
            JSONArray jsonArray = result.optJSONArray("data");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject data = jsonArray.getJSONObject(i);
                int x = KJ(data);
                integers.add(x);
                this.data.add(new BeanMore(
                        data.optString("uniquekey"),
                        data.optString("title"),
                        data.optString("date"),
                        data.optString("category"),
                        data.optString("author_name"),
                        data.optString("url"),
                        data.optString("thumbnail_pic_s"),
                        data.optString("thumbnail_pic_s02"),
                        data.optString("thumbnail_pic_s03"),
                        x
                ));
            }
            Log.e("xxxz", integers.toString());
        } catch (JSONException e) {
            e.printStackTrace();
            Log.e("xxxz", "11zzzz" + "\n" + e);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}
